package com.zadtek.shopmall.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseSectionQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.bean.SectionTypeBean;
import com.zadtek.shopmall.bean.SubType;

import java.util.List;

/**
 * Created by Zackv on 2018/4/5.
 */

public class TypeAdapter extends BaseSectionQuickAdapter<SectionTypeBean,BaseViewHolder> {

    private Context context;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param layoutResId      The layout resource id of each item.
     * @param sectionHeadResId The section head layout id for each item
     * @param data             A new list is created out of this one to avoid mutable list
     */
    public TypeAdapter(Context context,int layoutResId, int sectionHeadResId, List<SectionTypeBean> data) {
        super(layoutResId, sectionHeadResId, data);
        this.context = context;
    }



    @Override
    protected void convertHead(BaseViewHolder helper, SectionTypeBean item) {
        helper.setText(R.id.title,item.header);
    }

    @Override
    protected void convert(BaseViewHolder helper, SectionTypeBean item) {
        SubType.SubTypeItem subTypeItem = item.t;
        helper.setText(R.id.sub_type_text,subTypeItem.getTypeName());
        Glide.with(context).load(subTypeItem.getTypeImg()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.sub_type_img));
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    if(getData().get(position).isHeader){
                     return 3;
                    }else{
                     return 1;
                    }
                }
            });
        }
    }
}
